package com.zk.mall.service.impl;

import com.zk.mall.dao.OrderDao;
import com.zk.mall.entity.Order;
import com.zk.mall.service.OrderService;
import com.zk.mall.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderServiceImpl implements OrderService {
    SqlSession session = SqlSessionFactoryUtil.getSqlSession();

    @Override
    public List<Order> getallOrder(int pageindex, int pagesize) {
        List<Order> orderList = new ArrayList<>();
        OrderDao orderDao = session.getMapper(OrderDao.class);
        Map map = new HashMap();
        map.put("pageindex", pagesize * (pageindex - 1));
        map.put("pagesize", pagesize);
        orderList = orderDao.getallOrder(map);
        return orderList;
    }

    @Override
    public boolean addOrder(Order order) {
        boolean flag = false;
        try {
            OrderDao orderDao = session.getMapper(OrderDao.class);
            flag = orderDao.addOrder(order);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean updateOrder(Order order) {
        boolean flag = false;
        try {
            OrderDao orderDao = session.getMapper(OrderDao.class);
            flag = orderDao.updateOrder(order);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean deleteOrder(Order order) {
        boolean flag = false;
        try {
            OrderDao orderDao = session.getMapper(OrderDao.class);
            flag = orderDao.deleteOrder(order);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public int getOrderCount() {
        OrderDao orderDao = session.getMapper(OrderDao.class);
        return orderDao.getOrderCount();
    }
}
